/*     */ package com.zimbra.cs.service.account;
/*     */ 
/*     */ import com.google.common.collect.Sets;
/*     */ import com.zimbra.common.service.ServiceException;
/*     */ import com.zimbra.common.soap.AccountConstants;
/*     */ import com.zimbra.common.soap.Element;
/*     */ import com.zimbra.cs.account.AccessManager;
/*     */ import com.zimbra.cs.account.Account;
/*     */ import com.zimbra.cs.account.Entry;
/*     */ import com.zimbra.cs.account.Group;
/*     */ import com.zimbra.cs.account.NamedEntry;
/*     */ import com.zimbra.cs.account.accesscontrol.Right;
/*     */ import com.zimbra.cs.account.accesscontrol.RightManager;
/*     */ import com.zimbra.cs.account.accesscontrol.TargetType;
/*     */ import com.zimbra.cs.account.accesscontrol.UserRight;
/*     */ import com.zimbra.cs.util.AccountUtil;
/*     */ import com.zimbra.soap.ZimbraSoapContext;
/*     */ import java.util.List;
/*     */ import java.util.Locale;
/*     */ import java.util.Map;
/*     */ import java.util.Map.Entry;
/*     */ import java.util.Set;
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ public class DiscoverRights
/*     */   extends AccountDocumentHandler
/*     */ {
/*  56 */   private static final Set<String> DELEGATED_SEND_RIGHTS = Sets.newHashSet(new String[] { "sendAs", "sendOnBehalfOf", "sendAsDistList", "sendOnBehalfOfDistList" });
/*     */   
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */   public Element handle(Element request, Map<String, Object> context)
/*     */     throws ServiceException
/*     */   {
/*  67 */     ZimbraSoapContext zsc = getZimbraSoapContext(context);
/*  68 */     Account account = getRequestedAccount(zsc);
/*     */     
/*  70 */     if (!canAccessAccount(zsc, account)) {
/*  71 */       throw ServiceException.PERM_DENIED("can not access account");
/*     */     }
/*     */     
/*  74 */     RightManager rightMgr = RightManager.getInstance();
/*  75 */     Set<Right> rights = Sets.newHashSet();
/*  76 */     for (Element eRight : request.listElements("right")) {
/*  77 */       UserRight r = rightMgr.getUserRight(eRight.getText());
/*  78 */       rights.add(r);
/*     */     }
/*     */     
/*  81 */     if (rights.size() == 0) {
/*  82 */       throw ServiceException.INVALID_REQUEST("no right is specified", null);
/*     */     }
/*     */     
/*  85 */     Element response = zsc.createElement(AccountConstants.DISCOVER_RIGHTS_RESPONSE);
/*  86 */     discoverRights(account, rights, response, true);
/*     */     
/*  88 */     return response;
/*     */   }
/*     */   
/*     */   public static boolean isDelegatedSendRight(Right right) {
/*  92 */     return DELEGATED_SEND_RIGHTS.contains(right.getName());
/*     */   }
/*     */   
/*     */   public static void discoverRights(Account account, Set<Right> rights, Element eParent, boolean onMaster) throws ServiceException
/*     */   {
/*  97 */     AccessManager accessMgr = AccessManager.getInstance();
/*  98 */     Map<Right, Set<Entry>> discoveredRights = accessMgr.discoverUserRights(account, rights, onMaster);
/*     */     
/* 100 */     Locale locale = account.getLocale();
/* 101 */     for (Map.Entry<Right, Set<Entry>> targetsForRight : discoveredRights.entrySet()) {
/* 102 */       Right right = (Right)targetsForRight.getKey();
/* 103 */       Set<Entry> targets = (Set)targetsForRight.getValue();
/*     */       
/* 105 */       List<Entry> sortedTargets = Entry.sortByDisplayName(targets, locale);
/*     */       
/* 107 */       isDelegatedSendRight = isDelegatedSendRight(right);
/*     */       
/* 109 */       eTargets = eParent.addElement("targets");
/* 110 */       eTargets.addAttribute("right", right.getName());
/* 111 */       for (Entry target : sortedTargets) {
/* 112 */         TargetType targetType = TargetType.getTargetType(target);
/* 113 */         Element eTarget = eTargets.addElement("target");
/* 114 */         eTarget.addAttribute("type", targetType.getCode());
/* 115 */         if (isDelegatedSendRight) {
/* 116 */           if (((target instanceof Account)) || ((target instanceof Group))) {
/* 117 */             String[] addrs = AccountUtil.getAllowedSendAddresses((NamedEntry)target);
/* 118 */             NamedEntry entry = (NamedEntry)target;
/* 119 */             for (String addr : addrs) {
/* 120 */               Element eEmail = eTarget.addElement("email");
/* 121 */               eEmail.addAttribute("addr", addr);
/*     */             }
/* 123 */             if ((target instanceof Account)) {
/* 124 */               eTarget.addAttribute("d", ((Account)entry).getDisplayName());
/* 125 */             } else if ((target instanceof Group)) {
/* 126 */               eTarget.addAttribute("d", ((Group)entry).getDisplayName());
/*     */             }
/*     */           } else {
/* 129 */             throw ServiceException.FAILURE("internal error, target for  delegated send rights must be account or group", null);
/*     */           }
/*     */           
/*     */         }
/* 133 */         else if ((target instanceof NamedEntry)) {
/* 134 */           NamedEntry entry = (NamedEntry)target;
/* 135 */           eTarget.addAttribute("id", entry.getId());
/* 136 */           eTarget.addAttribute("name", entry.getName());
/* 137 */           if ((target instanceof Account)) {
/* 138 */             eTarget.addAttribute("d", ((Account)entry).getDisplayName());
/* 139 */           } else if ((target instanceof Group)) {
/* 140 */             eTarget.addAttribute("d", ((Group)entry).getDisplayName());
/*     */           }
/*     */         } else {
/* 143 */           eTarget.addAttribute("name", target.getLabel());
/*     */         }
/*     */       }
/*     */     }
/*     */     boolean isDelegatedSendRight;
/*     */     Element eTargets;
/*     */   }
/*     */ }


/* Location:              /home/mint/zimbrastore.jar!/com/zimbra/cs/service/account/DiscoverRights.class
 * Java compiler version: 7 (51.0)
 * JD-Core Version:       0.7.1
 */